clear all
set more off 
clear matrix

* Exploit ISMU data 2001-2016 						  
* Source: Indagine_campionaria_immigrazione_in_Lombardia_YEAR.csv  
* This version: July 2017
* Reference: https://www.ismu.org/dati-sulle-migrazioni/


global ismu "$data\ismu"
global vars_tokeep year province municipality w_pro w_reg gender yob pob      /*
		 */ citizenship civil_status religion education permit permit_type    /*
		 */ entry_visa entry_date entry_contact civil_registry housing        /*
		 */ living_with spouse_nationality chldrn_tot chldrn_cohab chldrn_ita /*
		 */ chldrn_ita_minor family_size family_income family_remittances     /*
		 */ exp_foodclothes exp_remittances exp_savings work_status work_hours/*
		 */ job income exp_house exp_other job_find job_length transfer       /*
		 */ transfer_date participation info_italy info_home sanatorie 	      /*
		 */ income_min reunification_past reunification_future place_worship  /*
		 */ ask_help friends foreign_friends italian italian_home italian_work/*
		 */ italian_freetime integration_citiz integration_vote  			  /*
		 */ integration_title integration_work integration_culture 			  /*
		 */ integration_language integration_discrim df_2006 neighbors 		  /*
		 */ italian_understand italian_speak italian_read italian_write		  /*
		 */ edu_recog df_2007 voting reg_status integration_migrants 		  /*
		 */ integration_school integration_children integration_minors		  /*
		 */ marr_year spouse_yob spouse_yofentry spouse_workstat training 	  /*
		 */ opportunities associations

		
* Import data

forvalues i=2001(1)2016 {
	import delimited "$ismu\Indagine_campionaria_immigrazione_in_Lombardia_`i'.csv", encoding(UTF8) clear
	save "$ismu\ISMU_`i'.dta", replace
}
	 
		 
* Prepare data

* 2001 

use "$ismu\ISMU_2001.dta", clear
gen     year = 2001
order   year, first
rename  prov province
rename  comune municipality
gen     w_pro =.
rename  pesi_reg w_reg
rename  sesso gender
rename  anno_nas yob
replace yob = yob+1900
rename  cittad citizenship
rename  stato_ci civil_status
rename  istruz education
rename  perm_sog permit
rename  tipo_p_s permit_type
rename  visto entry_visa
replace entry_visa = 1 if entry_visa<7 & entry_visa!=.
replace entry_visa = 0 if entry_visa==7
rename  arr_ita entry_date
gen     entry_contact = .
replace entry_date = entry_date+1900 if entry_date<100
rename  anagrafe civil_registry
rename  alloggio housing
gen     living_with=.
replace living_with = 1 if conviv==1 & fg_conv==0
replace living_with = 2 if (conviv==2 | conviv==3) & fg_conv==0
replace living_with = 3 if (conviv==4 | conviv==5) & fg_conv==0
replace living_with = 4 if conviv==6 & fg_conv==0
replace living_with = 5 if (conviv==7 | conviv==8) & fg_conv==0
replace living_with = 6 if conviv==9 & fg_conv==0
replace living_with = 7 if conviv==10 & fg_conv==0
replace living_with = 8 if conviv==1 & fg_conv>0 & fg_conv!=.
replace living_with = 9 if (conviv==2 | conviv==3) & fg_conv>0 & fg_conv!=.
replace living_with = 10 if (conviv==4 | conviv==5) & fg_conv>0 & fg_conv!=.
replace living_with = 11 if conviv==6 & fg_conv>0 & fg_conv!=.
replace living_with = 12 if (conviv==7 | conviv==8) & fg_conv>0 & fg_conv!=.
replace living_with = 13 if conviv==9 & fg_conv>0 & fg_conv!=.
replace living_with = 14 if conviv==10 & fg_conv>0 & fg_conv!=.
replace living_with = 99 if conviv==99
rename  naz_con spouse_nationality
rename  fg_tot chldrn_tot
rename  fg_conv chldrn_cohab
rename  fg_it chldrn_ita
rename  fg_it_mi chldrn_ita_minor 
gen     family_size =.
gen     exp_foodclothes =.
gen     exp_house =.
gen     exp_other =.
gen     exp_remittances =.
gen     exp_savings =.
rename  cond_la1 work_status
gen     job =.
rename  reddito income
rename  trov_lav job_find
rename  temp_lav job_length
rename	trasfer transfer
rename	temp_tra transfer_date
rename	imp_part participation  
save    "ISMU.dta", replace



*** 2002
use "$ismu\ISMU_2002.dta", clear
gen     year = 2002
order   year, first
rename  prov province
rename  comune municipality
rename  pesi_pro w_pro
rename  pesi_reg w_reg
rename  genere gender   
rename  nascita yob
rename  nazion citizenship
rename  st_civ civil_status
rename  relig religion
rename  tit_stua education
rename  ps permit
replace permit = 3 if permit==4
rename  tipo_ps permit_type
rename  visto entry_visa
replace entry_visa = 1 if entry_visa<8 & entry_visa!=.
replace entry_visa = 0 if entry_visa==8
rename  arr_ita entry_date
rename  pre_nes entry_contact
replace entry_contact = 1-entry_contact
rename  anagrafe civil_registry
rename  alloggio housing
rename  con_vive living_with
rename  naz_cg spouse_nationality
rename  n_figl_t chldrn_tot
rename  n_figl_c chldrn_cohab
rename  n_figl_i chldrn_ita
rename  n_fig_im chldrn_ita_minor
gen     family_size =.
gen     exp_foodclothes =.
gen     exp_house =.
gen     exp_other =.
gen     exp_remittances =.
gen     exp_savings =.
rename  cond_1 work_status
gen     job =. // Present but no clue on how is coded
rename  reddito income
rename	come_lav job_find
rename  trasfer transfer 
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2003
use "$ismu\ISMU_2003.dta", clear
gen     year = 2003
order   year, first
rename  prov province
rename  comune municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  genere gender   
rename  nascita yob
rename  naz citizenship
rename  st_civ civil_status
rename  relig religion
rename  studio1 education
gen     permit =.
replace permit = 1 if pds==1
replace permit = 2 if pds==2 | pds==5 
replace permit = 3 if pds==4 | pds==6 | pds==7
replace permit = 4 if pds==3
replace permit = 99 if pds==99
rename  tipo_pds permit_type
rename  visto entry_visa
replace entry_visa = 1 if entry_visa<8 & entry_visa!=.
replace entry_visa = 0 if entry_visa==8
rename  arr_ita entry_date
rename  rete entry_contact
replace entry_contact = 0 if entry_contact==1
replace entry_contact = 1 if entry_contact>1 & entry_contact<99 & entry_contact!=.
rename  anagrafe civil_registry
rename  alloggio housing
rename  con_vive living_with
rename  naz_cg spouse_nationality
rename  figli_to chldrn_tot
rename  figli_it chldrn_cohab
rename  figli_co chldrn_ita
rename  figli_cm chldrn_ita_minor 
gen     family_size =.
gen     exp_foodclothes =.
gen     exp_house =.
gen     exp_other =.
gen     exp_remittances =.
gen     exp_savings =.
rename  cond_la1 work_status
replace work_status=13 if work_status==12
rename  lavoro job
rename  reddito income
gen 	sanatorie = .
forvalues i=1(1)7 {
		replace sanatorie = `i'-1 if sanat`i'==1
}
replace sanatorie = 99 if sanatorie==6
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2004
use "$ismu\ISMU_2004.dta", clear
gen     year = 2004
order   year, first
rename  prov province
rename  codcom municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  genere gender   
rename  nascita yob
rename  cittad citizenship
rename  statciv civil_status
replace civil_status =. if civil_status==602
rename  rel religion
rename  titstud education
gen     permit =.
replace permit = 1 if permsog==1
replace permit = 2 if permsog==2 | permsog==5 
replace permit = 3 if permsog==4 | permsog==6 | permsog==7
replace permit = 4 if permsog==3
replace permit = 99 if permsog==99
rename  permsog2 permit_type
gen     entry_visa =.
rename  arr_ita entry_date
gen     entry_contact =.
rename  anagr civil_registry
rename  casa housing
rename  conv1 living_with
replace living_with = conv2 if missing(living_with)
gen     spouse_nationality =.
replace figli1 =. if figli1==99
rename  figli1 chldrn_tot
replace figli2 =. if figli2==99
rename  figli2 chldrn_cohab
replace figli3 =. if figli3==99
rename  figli3 chldrn_ita
replace figli4 =. if figli4==99
rename  figli4 chldrn_ita_minor 
rename  nucleo family_size
rename  spesa1 exp_foodclothes
rename  spesa2 exp_house
rename  spesa3 exp_other
rename  spesa4 exp_remittances
rename  spesa5 exp_savings
rename  lavoro work_status
replace work_status=13 if work_status==12
rename  lavoro2 job
rename  reddito income
gen     sanatorie = .
replace sanatorie = 0 if bosfi1==2
replace sanatorie = 5 if bosfi1==1
rename 	salute1 ask_help  
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2005
use "$ismu\ISMU_2005.dta", clear
gen     year = 2005
order   year, first
rename  prov province
rename  c municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d3 citizenship
rename  d8 civil_status
gen     religion=.
replace religion = 1 if d10==2
replace religion = 2 if d10==1
replace religion = 3 if d10==5 | d10==3 | d10==4
replace religion = 4 if d10==6
replace religion = 5 if d10==7
replace religion = 6 if d10==9 | d10==8
replace religion = 7 if d10==10
replace religion = 99 if d10==99
rename  d9 education
gen     permit =.
replace permit = 1 if d11==1
replace permit = 2 if d11==2 | d11==3 
replace permit = 3 if d11==4 | d11==5 
replace permit = 4 if d11==6
replace permit = 99 if d11==99
rename  d12 permit_type
rename  d7 entry_visa
replace entry_visa = 1 if entry_visa<8 & entry_visa!=.
replace entry_visa = 0 if entry_visa==8 | entry_visa==9
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d13 civil_registry
rename  d14 housing
rename  d15 living_with
rename  d29 spouse_nationality
replace d16a =. if d16a==99
rename  d16a chldrn_tot
replace d16b =. if d16b==99
rename  d16b chldrn_cohab
replace d16c =. if d16c==99
rename  d16c chldrn_ita
replace d16d =. if d16d==99
rename  d16d chldrn_ita_minor 
rename  d17d family_size
rename  d17a exp_foodclothes
rename  d17b exp_house
rename  d17c exp_other
rename  d17e exp_remittances
rename  d17f exp_savings
rename  d18a work_status
rename  d21 job
rename  d22 income
rename  d17g income_min
gen 	reunification_past = .
foreach i in 1 2 3 4 5 99 {
		replace reunification_past = `i' if d23`i'==1
}
gen 	reunification_future = .
foreach i in 1 2 3 4 99 {
		replace reunification_future = `i' if d24`i'==1
}
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2006
use "$ismu\ISMU_2006.dta", clear
foreach var of varlist _all {
	replace `var' = . if `var'==-1
}
gen     year = 2006
order   year, first
rename  prov province
rename  c municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d2b pob
rename  d3 citizenship
rename  d7 civil_status
gen     religion=.
replace religion = 1 if d9==2
replace religion = 2 if d9==1
replace religion = 3 if d9==5 | d9==3 | d9==4
replace religion = 4 if d9==6
replace religion = 5 if d9==7
replace religion = 6 if d9==9 | d9==8
replace religion = 7 if d9==10
replace religion = 99 if d9==99
rename  d8 education
gen     permit =.
replace permit = 1 if d11==1
replace permit = 2 if d11==2 | d11==3 
replace permit = 3 if d11==4 | d11==5 
replace permit = 4 if d11==6
replace permit = 99 if d11==7
rename  d12 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d13 civil_registry
rename  d14 housing
rename  d16 living_with
rename  d17 spouse_nationality
replace d15a =. if d15a==99
rename  d15a chldrn_tot
replace d15b =. if d15b==99
rename  d15b chldrn_cohab
replace d15c =. if d15c==99
rename  d15c chldrn_ita
replace d15d =. if d15d==99
rename  d15d chldrn_ita_minor 
rename  d18d family_size
rename  d18a exp_foodclothes
rename  d18b exp_house
rename  d18c exp_other
rename  d18e exp_remittances
rename  d18f exp_savings
rename  d19 work_status
replace work_status = 13 if work_status==14
rename  d22 job
replace job = 1 if job==10
replace job = 2 if job==20
rename  d23 income  
rename  d8b edu_recog
rename 	d10 place_worship
rename  d18g income_min
gen 	reunification_past = .
foreach i in 1 2 3 4 5 99 {
		replace reunification_past = `i' if d24`i'==1
}
gen 	reunification_future = .
foreach i in 1 2 3 4 99 {
		replace reunification_future = `i' if d25`i'==1
}
rename  d30 friends
rename  d31 foreign_friends
rename  d32 italian
rename  d33a italian_home
rename  d33b italian_work
rename  d33c italian_freetime
rename 	d341 integration_citiz
rename 	d342 integration_vote
rename 	d343 integration_title
rename 	d344 integration_work
rename 	d345 integration_culture
rename 	d346 integration_language
rename 	d347 integration_discrim
rename  d35 df_2006
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2007
use "$ismu\ISMU_2007.dta", clear
gen     year = 2007
order   year, first
rename  prv province
rename  comune municipality
rename  pesi_prv w_pro
rename  pesi_reg w_reg
rename  genere gender   
rename  a_nascit yob
rename  l_nascita pob
rename  citt citizenship
rename  st_civ civil_status
gen     religion=.
replace religion = 1 if relig==2
replace religion = 2 if relig==1
replace religion = 3 if relig==5 | relig==3 | relig==4
replace religion = 4 if relig==6
replace religion = 5 if relig==7
replace religion = 6 if relig==9 | relig==8
replace religion = 7 if relig==10
replace religion = 99 if relig==99
rename  tit_stu education
gen     permit =.
replace permit = 1 if cond_giu==2
replace permit = 2 if cond_giu==3 | cond_giu==4 | cond_giu==5 | cond_giu==6
replace permit = 3 if cond_giu==7 | cond_giu==8 | cond_giu==9
replace permit = 4 if cond_giu==1
rename  tipo_ps permit_type
gen     entry_visa =.
rename  a_italia entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  anagraf civil_registry
rename  alloggio housing
rename  con_vive living_with
rename  naz_con spouse_nationality
rename  f_tot chldrn_tot
rename  f_conv chldrn_cohab
rename  f_ita chldrn_ita
rename  f_minor chldrn_ita_minor 
rename  nr_nucle family_size
rename 	redd_ncl family_income
rename  s_al_ab exp_foodclothes
rename  s_casa exp_house
rename  s_altro exp_other
rename  i_rim exp_remittances
rename  i_risp exp_savings
rename  c_lavor work_status
replace work_status = 13 if work_status==14
rename  tipo_lav job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  redd_0 income 
rename  d8b edu_recog
rename  vicini neighbors
rename  info_ita info_italy
rename  info_pae info_home
rename  amici friends
rename  amici_s foreign_friends
rename 	ita_comp italian_understand
rename	ita_parl italian_speak
rename	ita_legg italian_read
rename	ita_scri italian_write
rename  casa_ita italian_home
rename  lavo_ita italian_work
rename  tlib_ita italian_freetime
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2008
use "$ismu\ISMU_2008.dta", clear
gen     year = 2008
order   year, first
rename  a province
rename  c municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d2b pob
rename  d3 citizenship
rename  d7 civil_status
gen     religion=.
replace religion = 1 if d9==2
replace religion = 2 if d9==1
replace religion = 3 if d9==6 | d9==3 | d9==4 | d9==5
replace religion = 4 if d9==7
replace religion = 5 if d9==8
replace religion = 6 if d9==9 | d9==10
replace religion = 7 if d9==11
replace religion = 99 if d9==99
rename  d8 education
gen     permit =.
replace permit = 1 if d10==2
replace permit = 2 if d10==3 | d10==4 | d10==5 | d10==6
replace permit = 3 if d10==7 | d10==8 | d10==9
replace permit = 4 if d10==1
rename  d11 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d12 civil_registry
rename  d13 housing
replace housing = 99 if housing==14
replace housing = 14 if housing==15
replace housing = 15 if housing==16
replace housing = 16 if housing==99
rename  d15 living_with
rename  d17 spouse_nationality
rename  d14a chldrn_tot
rename  d14c chldrn_cohab
rename  d14b chldrn_ita
rename  d14d chldrn_ita_minor 
rename  d19 family_size
rename  d20 family_income
rename  d21a exp_foodclothes
rename  d21b exp_house
rename  d21c exp_other
rename  d21d exp_remittances
rename  d21e exp_savings
rename  d22 work_status
replace work_status = 13 if work_status==14
rename  d23 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d24 income 
rename  d8b edu_recog
rename  d11b df_2007
rename  d30 voting
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2009
use "$ismu\ISMU_2009.dta", clear
gen     year = 2009
order   year, first
rename  a province
rename  b municipality
rename  p_prov w_pro
rename  p_reg w_reg
rename  d1 gender   
rename  d2 yob
rename  d2b pob
rename  d3 citizenship
rename  d7 civil_status
gen     religion=.
replace religion = 1 if d10==2
replace religion = 2 if d10==1
replace religion = 3 if d10==6 | d10==3 | d10==4 | d10==5
replace religion = 4 if d10==7
replace religion = 5 if d10==8
replace religion = 6 if d10==9 | d10==10
replace religion = 7 if d10==11
replace religion = 99 if d10==99
rename  d9 education
gen     permit =.
replace permit = 1 if d11==2
replace permit = 2 if d11==3 | d11==4 | d11==5 | d11==6
replace permit = 3 if d11==7 | d11==8 | d11==9
replace permit = 4 if d11==1
rename  d12 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d13 civil_registry
rename  d14 housing
replace housing = 16 if housing==14
replace housing = 14 if housing==15
rename  vive_con living_with
rename  d8 spouse_nationality
rename  d15a chldrn_tot
rename  d15c chldrn_cohab
rename  d15b chldrn_ita
rename  d15d chldrn_ita_minor
rename  d17 family_size
rename  d18 family_income
rename  d19a exp_foodclothes
rename  d19b exp_house
rename  d19c exp_other
rename  d19d exp_remittances
rename  d19e exp_savings 
rename  d20 work_status
replace work_status = 13 if work_status==14 | work_status==21
rename  d21 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d22 income
rename  d12bis reg_status
rename  d23 voting
rename  d25 integration_migrants
rename  d30 integration_school
rename  d31 integration_children
rename  d32 integration_minors
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2010
use "$ismu\ISMU_2010.dta", clear
gen     year = 2010
order   year, first 
gen		province = .
replace province = 12 if prov=="VA"
replace province = 13 if prov=="CO"
replace province = 14 if prov=="SO"
replace province = 15 if strpos(prov,"Mi")>0
replace province = 16 if prov=="BG"
replace province = 17 if prov=="BS"
replace province = 18 if prov=="PV"
replace province = 19 if prov=="CR"
replace province = 20 if prov=="MN"
replace province = 97 if prov=="LC"
replace province = 98 if prov=="LO"
replace province = 108 if prov=="MB"
rename  b municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d2b pob
rename  d3 citizenship
rename  d7 civil_status
gen     religion=.
replace religion = 1 if d10==2
replace religion = 2 if d10==1
replace religion = 3 if d10==6 | d10==3 | d10==4 | d10==5
replace religion = 4 if d10==7
replace religion = 5 if d10==8
replace religion = 6 if d10==9 | d10==10
replace religion = 7 if d10==11
replace religion = 99 if d10==99
rename  d9 education
gen     permit =.
replace permit = 1 if d11==3
replace permit = 2 if d11==4 | d11==5
replace permit = 3 if d11==6 | d11==7 | d11==8
replace permit = 4 if d11==1 | d11==2
rename  d12 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d13 civil_registry
rename  d14 housing
replace housing = 16 if housing==14
replace housing = 14 if housing==15
egen 	living_with_tot = rowtotal(d18a-d18g)
gen	  	living_with =.
replace living_with = 1 if d17==1
replace living_with = 2 if living_with_tot==d18a & d18a>0
replace living_with = 3 if living_with_tot==d18a+d18b+d18e & d18a>0 & d18b>0 & d18e>0
replace living_with = 4 if living_with_tot==d18b+d18e & d18b>0 & d18e>0
replace living_with = 5 if living_with_tot==d18a+d18f & d18a>0 & d18f>0
replace living_with = 6 if living_with_tot==d18b+d18e+d18f & d18b>0 & d18e>0 & d18f>0
replace living_with = 7 if living_with_tot==d18f & d18f>0
replace living_with = 8 if living_with_tot==d18c & d18c>0
replace living_with = 9 if living_with_tot==d18a+d18c & d18a>0 & d18c>0
replace living_with = 10 if living_with_tot==d18a+d18b+d18e+d18c & d18a>0 & d18b>0 & d18e>0 & d18c>0
replace living_with = 11 if living_with_tot==d18b+d18e+d18c & d18b>0 & d18e>0 & d18c>0
replace living_with = 12 if living_with_tot==d18a+d18f+d18c & d18a>0 & d18f>0 & d18c>0
replace living_with = 13 if living_with_tot==d18b+d18e+d18f+d18c & d18b>0 & d18e>0 & d18f>0 & d18c>0
replace living_with = 14 if living_with_tot==d18f+d18c & d18f>0 & d18c>0
rename  d8 spouse_nationality
rename  d15a chldrn_tot
rename  d15c chldrn_cohab
rename  d15b chldrn_ita
gen		chldrn_ita_minor =.
rename  d19 family_size
rename  d20 family_income
rename  d21a exp_foodclothes
rename  d21b exp_house
rename  d21c exp_other
rename  d21d exp_remittances
rename  d21e exp_savings 
rename  d22_1 work_status
replace d23 = d23+3 if d23!=99
replace work_status = . if work_status>3
replace work_status = d23 if work_status==.
replace work_status = 13 if work_status==14 | work_status==21
rename  d24 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d25 income
rename  d26 transfer
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2011
use "$ismu\ISMU_2011.dta", clear
gen     year = 2011
order   year, first
gen		province = .
replace province = 12 if provincia_13=="VA"
replace province = 13 if provincia_13=="CO"
replace province = 14 if provincia_13=="SO"
replace province = 15 if provincia_13=="AltriMI" | provincia_13=="Micitta"
replace province = 16 if provincia_13=="BG"
replace province = 17 if provincia_13=="BS"
replace province = 18 if provincia_13=="PV"
replace province = 19 if provincia_13=="CR"
replace province = 20 if provincia_13=="MN"
replace province = 97 if provincia_13=="LC"
replace province = 98 if provincia_13=="LO"
replace province = 108 if provincia_13=="MB"
rename  b municipality
rename  peso_prov w_pro
rename  peso_reg w_reg
rename  d1 gender   
rename  d2 yob
rename  d2b pob
rename  d3 citizenship
rename  d7 civil_status
drop	religion
gen     religion=.
replace religion = 1 if d10==2
replace religion = 2 if d10==1
replace religion = 3 if d10==6 | d10==3 | d10==4 | d10==5
replace religion = 4 if d10==7
replace religion = 5 if d10==8
replace religion = 6 if d10==9 | d10==10
replace religion = 7 if d10==11
replace religion = 99 if d10==99
rename  d9 education
gen     permit =.
replace permit = 1 if d11==3
replace permit = 2 if d11==4 | d11==5
replace permit = 3 if d11==6 | d11==7 | d11==8
replace permit = 4 if d11==1 | d11==2
rename  d12 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d13 civil_registry
rename  d14 housing
replace housing = 16 if housing==14
replace housing = 14 if housing==15
rename  d18 living_with
gen 	spouse_nationality =.
rename  d16a chldrn_tot
rename  d16c chldrn_cohab
rename  d16b chldrn_ita
rename 	d16d chldrn_ita_minor
rename  d21 family_size
rename  d22 family_income
rename  d23a exp_foodclothes
rename  d23b exp_house
rename  d23c exp_other
rename  d23d exp_remittances
rename  d23e exp_savings 
rename  d27oggi work_status
replace work_status = 2 if work_status==3
replace work_status = work_status - 1 if work_status>2
replace work_status = 13 if work_status==7 | work_status==8 | work_status==9
replace work_status = work_status-3 if work_status>6
replace work_status = -1 if work_status==4 
replace work_status = 4 if work_status==6
replace work_status = 6 if work_status==-1
replace work_status = 13 if work_status==14 
replace work_status = 99 if work_status>13
drop	job
rename  d28 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d29 income
rename  d30 transfer
rename  d40 marr_year
rename  d41 spouse_yob
rename  d43 spouse_yofentry
rename  d44 spouse_workstat
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2012
use "$ismu\ISMU_2012.dta", clear
gen     year = 2012
order   year, first
rename  prov province
rename  comune municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d2b pob
rename  d3 citizenship
rename  d7 civil_status
gen     religion=.
replace religion = 1 if d10==2
replace religion = 2 if d10==1 | d10==111 | d10==112 | d10==113
replace religion = 3 if d10==6 | d10==3 | d10==4 | d10==5
replace religion = 4 if d10==7
replace religion = 5 if d10==8
replace religion = 6 if d10==9 | d10==10
replace religion = 7 if d10==11
replace religion = 99 if d10==99
rename  d8 education
replace education = 2 if education==3
replace education = 3 if education==4
replace education = 4 if education==5
rename  d9 training
gen     permit =.
replace permit = 1 if d11==3
replace permit = 2 if d11==4 | d11==5
replace permit = 3 if d11==6 | d11==7 | d11==8
replace permit = 4 if d11==1 | d11==2
rename  d12 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d13 civil_registry
rename  d14 housing
rename  d16 living_with
rename  d18 spouse_nationality
replace spouse_nationality =. if spouse_nationality==4
rename  d15a chldrn_tot
rename  d15c chldrn_cohab
rename  d15b chldrn_ita
rename  d15d chldrn_ita_minor
rename  d19 family_size
rename  d20 family_income
rename  d21a exp_foodclothes
rename  d21b exp_house
rename  d21c exp_other
rename  d21d exp_remittances
rename  d21e exp_savings 
rename  d24_og work_status
replace work_status = 2 if work_status==3
replace work_status = work_status - 1 if work_status>2
replace work_status = 13 if work_status==7 | work_status==8 | work_status==9
replace work_status = work_status-3 if work_status>6
replace work_status = -1 if work_status==4 
replace work_status = 4 if work_status==6
replace work_status = 6 if work_status==-1
replace work_status = 13 if work_status==14 
replace work_status = 99 if work_status>13
rename  d25 work_hours
rename  d26 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d27 income 
rename  d28 transfer
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2013
use "$ismu\ISMU_2013.dta", clear
gen     year = 2013
order   year, first
rename  prov2 province
rename  comune municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d3 pob
rename  d7 citizenship
rename  d8 civil_status
replace d11 = 1 if d11==2 | d11==3
replace d11 = d11 - 2 if d11>1 & d11<99
gen     religion=.
replace religion = 1 if d11==2
replace religion = 2 if d11==1 
replace religion = 3 if d11==6 | d11==3 | d11==4 | d11==5
replace religion = 4 if d11==7
replace religion = 5 if d11==8
replace religion = 6 if d11==9 | d11==10
replace religion = 7 if d11==11
replace religion = 99 if d11==99
rename  d10 education
replace education = 2 if education==3
replace education = 3 if education==4
replace education = 4 if education==5 | education==51 | education==52 | education==53
gen     permit =.
replace permit = 1 if d12==3
replace permit = 2 if d12==4 | d12==5
replace permit = 3 if d12==6 | d12==7 | d12==8
replace permit = 4 if d12==1 | d12==2
rename  d13 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d14 civil_registry
rename  d15 housing
rename  d17 living_with
rename  d9 spouse_nationality
replace spouse_nationality=. if spouse_nationality==4
rename  d16a chldrn_tot
rename  d16c chldrn_cohab
rename  d16b chldrn_ita
rename  d16d chldrn_ita_minor
rename  d19 family_size
rename  d20 family_income
rename  d21 family_remittances
gen		exp_foodclothes =.
gen		exp_house =.
gen 	exp_other =.
gen 	exp_remittances =.
gen		exp_savings =.
rename  d23a work_status
replace work_status = 2 if work_status==3
replace work_status = work_status - 1 if work_status>2
replace work_status = 13 if work_status==7 | work_status==8 | work_status==9
replace work_status = work_status-3 if work_status>6
replace work_status = -1 if work_status==4 
replace work_status = 4 if work_status==6
replace work_status = 6 if work_status==-1
replace work_status = 13 if work_status==14 
replace work_status = 99 if work_status>13
rename  d24 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d25 income
rename  d26 transfer
rename 	d27a italian_understand
rename	d27b italian_speak
rename	d27c italian_read
rename	d27d italian_write
rename  d28a italian_home
rename  d28b italian_work
rename  d28c italian_freetime
rename  d32 friends
rename  d33 associations
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2014
use "$ismu\ISMU_2014.dta", clear
gen     year = 2014
order   year, first
rename  prov province
rename  b municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename 	d3 pob
rename  d7 citizenship
rename  d8 civil_status
replace d13 = 1 if d13==2 | d13==3
replace d13 = d13 - 2 if d13>1 & d13<99
gen     religion=.
replace religion = 1 if d13==2
replace religion = 2 if d13==1 | d13==111 | d13==112 | d13==113
replace religion = 3 if d13==6 | d13==3 | d13==4 | d13==5
replace religion = 4 if d13==7
replace religion = 5 if d13==8
replace religion = 6 if d13==9 | d13==10
replace religion = 7 if d13==11
replace religion = 99 if d13==99
rename  d12 education
replace education = 2 if education==3
replace education = 3 if education==4
replace education = 4 if education==5
gen     permit =.
replace permit = 1 if d14==3
replace permit = 2 if d14==4 | d14==5
replace permit = 3 if d14==6 | d14==7 | d14==8
replace permit = 4 if d14==1 | d14==2
rename  d15 permit_type
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d16 civil_registry
rename  d17 housing
rename  d19 living_with
rename  d9 spouse_nationality
replace spouse_nationality=. if spouse_nationality==98
rename  d18a chldrn_tot
rename  d18c chldrn_cohab
rename  d18b chldrn_ita
rename  d18d chldrn_ita_minor
rename  d22 family_size
rename  d23 family_income
rename  d25 family_remittances
gen		exp_foodclothes =.
gen		exp_house =.
gen 	exp_other =.
gen 	exp_remittances =.
gen		exp_savings =.
rename  d27a work_status
replace work_status = 2 if work_status==3
replace work_status = work_status - 1 if work_status>2
replace work_status = 13 if work_status==7 | work_status==8 | work_status==9
replace work_status = work_status-3 if work_status>6
replace work_status = -1 if work_status==4 
replace work_status = 4 if work_status==6
replace work_status = 6 if work_status==-1
replace work_status = 13 if work_status==14 
replace work_status = 99 if work_status>13
rename  d28 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d29 income
rename  d31 transfer
gen 	spouse_workstat =.
replace spouse_workstat = 1 if d10==3 | d10==4
replace spouse_workstat = 2 if d10==1 | d10==2
replace spouse_workstat = 99 if d10==99
rename  d11 spouse_entry
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2015
use "$ismu\ISMU_2015.dta", clear
gen     year = 2015
order   year, first
rename  prov province
rename  b municipality
rename  pesiprov w_pro
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d3 pob
rename  d7 citizenship
rename  d8 civil_status
gen     religion=.
replace religion = 1 if d13==2
replace religion = 2 if d13==1
replace religion = 3 if d13==6 | d13==3 | d13==4 | d13==5
replace religion = 4 if d13==7
replace religion = 5 if d13==8
replace religion = 6 if d13==9 | d13==10
replace religion = 7 if d13==11
replace religion = 99 if d13==99
drop	education
rename  d9 education
replace education = 1 if education==2
replace education = 2 if education==3 | education==4 
replace education = 3 if education==5 | education==6
replace education = 4 if education==7 | education==8
gen     permit =.
replace permit = 1 if d14==3
replace permit = 2 if d14==4 | d14==5
replace permit = 3 if d14==6 | d14==7 | d14==8
replace permit = 4 if d14==1 | d14==2
rename  d15 permit_type
replace permit_type = 5 if permit_type==6
replace permit_type = 6 if permit_type==7
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d16 civil_registry
replace	housing =.
rename  d19 living_with
rename  d20 spouse_nationality
replace spouse_nationality=. if spouse_nationality==98
rename  d18a chldrn_tot
rename  d18c chldrn_cohab
rename  d18b chldrn_ita
rename  d18d chldrn_ita_minor
rename  d22 family_size
rename  d23 family_income
rename  d25 family_remittances
gen		exp_foodclothes =.
gen		exp_house =.
gen 	exp_other =.
gen 	exp_remittances =.
gen		exp_savings =.
rename  d28a work_status
replace work_status = 2 if work_status==3
replace work_status = work_status - 1 if work_status>2
replace work_status = 13 if work_status==7 | work_status==8 | work_status==9
replace work_status = work_status-3 if work_status>6
replace work_status = -1 if work_status==4 
replace work_status = 4 if work_status==6
replace work_status = 6 if work_status==-1
replace work_status = 13 if work_status==14 
replace work_status = 99 if work_status>13
rename  d29 work_hours
rename  d30 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d32 income
rename  d33 transfer
rename 	d11a italian_understand
rename	d11b italian_speak
rename	d11c italian_read
rename	d11d italian_write
gen 	spouse_workstat =.
replace spouse_workstat = 1 if d21==3 | d21==4
replace spouse_workstat = 2 if d21==1 | d21==2
replace spouse_workstat = 99 if d21==99
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace



*** 2016
use "$ismu\ISMU_2016.dta", clear
gen     year = 2016
order   year, first
rename  prov province
rename  b municipality
rename  pesiprov w_pro
destring w_pro, force replace
rename  pesireg w_reg
rename  d1 gender   
rename  d2 yob
rename  d3 pob
rename  d7 citizenship
rename  d8 civil_status
gen     religion=.
replace religion = 1 if d11==2
replace religion = 2 if d11==1
replace religion = 3 if d11==6 | d11==3 | d11==4 | d11==5
replace religion = 4 if d11==7
replace religion = 5 if d11==8
replace religion = 6 if d11==9 | d11==10
replace religion = 7 if d11==11
replace religion = 99 if d11==99
drop	education
rename  d9 education
replace education = 2 if education==3
replace education = 3 if education==4 | education==5
replace education = 4 if education==6
rename  d9b edu_recog
gen     permit =.
replace permit = 1 if d12==3
replace permit = 2 if d12==4 | d12==5
replace permit = 3 if d12==6 | d12==7 | d12==8
replace permit = 4 if d12==1 | d12==2
rename  d13 permit_type
replace permit_type = 5 if permit_type==6 | permit_type==7 | permit_type==8
replace permit_type = 6 if permit_type==9
gen     entry_visa =.
rename  d4 entry_date
replace entry_date=. if entry_date==99
gen     entry_contact =.
rename  d15 civil_registry
replace	housing =.
rename  d21 living_with
rename  d23 spouse_nationality
replace spouse_nationality=. if spouse_nationality==98
rename  d20a chldrn_tot
rename  d20c chldrn_cohab
rename  d20b chldrn_ita
rename  d20d chldrn_ita_minor
rename  d25 family_size
rename  d26 family_income
rename  d27b family_remittances
gen		exp_foodclothes =.
gen		exp_house =.
gen 	exp_other =.
gen 	exp_remittances =.
gen		exp_savings =.
rename  d28a work_status
replace work_status = 2 if work_status==3
replace work_status = work_status - 1 if work_status>2
replace work_status = 13 if work_status==7 | work_status==8 | work_status==9
replace work_status = work_status-3 if work_status>6
replace work_status = -1 if work_status==4 
replace work_status = 4 if work_status==6
replace work_status = 6 if work_status==-1
replace work_status = 13 if work_status==14 
replace work_status = 99 if work_status>13
rename  d29 work_hours
rename  d30 job
replace job = 1 if job==10
replace job = 2 if job>=20 & job<=25 & job!=.
rename  d32 income 
rename  d33 transfer
rename 	d10 italian_understand
rename  d19 neighbors
gen 	spouse_workstat =.
replace spouse_workstat = 1 if d24==3 | d24==4
replace spouse_workstat = 2 if d24==1 | d24==2
replace spouse_workstat = 99 if d24==99
rename  d37 friends
rename  d39 opportunities
gen		associations =.
replace associations = 1 if d40a==1
replace associations = 2 if d40a==1
replace associations = 3 if d40c==1
replace associations = 4 if d40d==1
replace associations = 99 if d40e==1
append  using "$ismu\ISMU.dta", force
save    "$ismu\ISMU.dta", replace


* Final dataset

use "ISMU.dta", clear
keep $vars_tokeep  
order $vars_tokeep
foreach var of global vars_tokeep {
	cap replace `var'=. if `var'==99
}
label drop _all

*** Define value labels
label define prov        12 Varese 13 Como 14 Sondrio 15 Milano 16 Bergamo 17 Brescia 18 Pavia        /*
					  */ 19 Cremona 20 Mantova 97 Lecco 98 Lodi 108"Monza Brianza"
label define yesno 	     0 No 1 Yes 2 "Do not know" 99"n/d"
label define mf          1 Male  2 Female 
label define cit         201 Albania 202 Andorra 256 Bielorussia 252 "Bosnia-Erzegovina" 209 Bulgaria /*
                      */ 257 "Czech Rep." 250 Croazia 247 Estonia 224 "Jugoslavia Serbia-Mont."       /*
					  */ 248 Lettonia 249 Lituania 253 Macedonia 254 Moldavia 233 Polonia 235 Romania /*
					  */ 245 Russia 255 Slovacchia 251 Slovenia 351 Turchia 243 Ucraina 244 Ungheria  /*
					  */ 503 "Antigua e Barbuda" 602 Argentina 505 Bahamas 506 Barbados 507 Belize    /*
					  */ 604 Bolivia 605 Brasile 606 Cile 608 Colombia  513 Costarica 514 Cuba        /*
					  */ 515 Dominica 516 "Rep. Dominicana" 609 Ecuador 517 "El Salvador" 518 Giamaica/*
					  */ 519 Grenada 523 Guatemala  612 Guyana 524 Haiti 525 Honduras 527 Messico     /*
					  */ 529 Nicaragua 530 Panama 614 Paraguay 615 Perù 532 "Saint Lucia"             /*
					  */ 534 "Saint Kitts e Nevis" 533 "Saint Vincent e Grenadine" 616 Suriname       /*
					  */ 617 "Trinidad and Tobago" 618 Uruguay 619 Venezuela 401 Algeria 402 Angola   /*
					  */ 406 "Benin (Dahomey)" 408 Botswana 409 "Burkina Faso (Alto Volta)"           /*
					  */ 410 Burundi 411 Camerun 413 "Capo Verde" 414 "Centrafrican Rep." 415 Ciad    /*
					  */ 417 Comore 418 Congo 463 "Congo Dem. Rep. (Zaire)" 404 "Costa d'Avorio"      /*
					  */ 419 Egitto 466 Eritrea 420 Etiopia 421 Gabon 422 Gambia 423 Ghana 424 Gibuti /* 
					  */ 425 Guinea 426 "Guinea Bissau" 427 "Guinea Equatoriale" 428 Kenia 429 Lesotho/*
					  */ 430 Liberia 431 Libia 432 Madagascar 434 Malawi 435 Mali 436 Marocco         /*
					  */ 437 Mauritania 438 Maurizio 440 Mozambico 441 Namibia 442 Niger 443 Nigeria  /*
					  */ 446 Ruanda 448 "Sao Tome Principe" 449 Seicelle 450 Senegal                  /*
					  */ 451 "Sierra Leone" 453 Somalia 455 Sudan 456 Swaziland 457 Tanzania 458 Togo /*
					  */ 460 Tunisia 461 Uganda 464 Zambia 465 "Zimbabwe (Rhodesia)" 301 Afghanistan  /*
					  */ 302 "Arabia Saudita" 358 Armenia 359 Azerbaigian 304 Bahrein 305 Bangladesh  /*
					  */ 306 Bhutan 309 Brunei 310 Cambogia 314 Cina 319 "Corea del Nord"             /*
					  */ 322 "Emirati Arabi Uniti" 323 Filippine 360 Georgia 327 Giordania 330 India  /*
					  */ 331 Indonesia 332 Iran 333 Iraq 356 Kazakistan 361 Kirghizistan 335 Kuwait   /*
					  */ 336 Laos 337 Libano 321 Macao 339 Maldive 340 Malaysia 341 Mongolia          /*
					  */ 307 "Myanmar (Birmania)" 342 Nepal 343 Oman 344 Pakistan 324 Palestina       /*
					  */ 345 Qatar 348 Siria 311 "Sri Lanka (Ceylon)" 362 Tagikistan 349 Thailandia   /*
					  */ 364 Turkmenistan 357 Uzbekistan 353 Vietnam 354 Yemen 999 Apolide            /*
					  */ 346 Singapore 363 Taiwan 320 "Corea del Sud" 454 "Sud Africa"			      /*
					  */ 270 Montenegro 271 Serbia 272 Kosovo
label define pob		 1 Abroad 2 Italy
label define civstat     1"Unmarried" 2"Married" 3"Widow/er" 4"Divorced" 99"n/d"
label define rel         1 Catholic 2 Muslim 3"Other christian" 4 Buddhist 5 Hindu 6 Other 7 None     /*
					  */ 99"n/d"			  
label define educ        1"No title" 2"Compulsory education: <15" 3"Secondary school: 15-18"  	      /*
					  */ 4"Bachelor or higher" 99"n/d"
label define perm        1"Yes, carta di soggiorno"  2"Yes, permesso di soggiorno" 3"No"              /*
					  */ 4"Italian or EU citizenship" 99"n/d"
label define permt       1"Family" 2"Employee" 3"Self-employment" 4"Study or similar" 				  /*
					  */ 5"Temporary protection/refugee" 6"Other" 99"n/d"
label define anagrf      1"Yes, in this municipality" 2"Yes, in this province" 3"Yes, in Lombardia"	  /* 
					  */ 4"Yes, in another region"  5"No" 99"n/d"
label define house       1"Own house alone or with relatives" 										  /*
                      */ 2"Rent alone or with relatives - WITH CONTRACT" 							  /*
					  */ 3"Rent alone or with relatives - WITHOUT CONTRACT"         	              /*
				      */ 4"Rent alone or with relatives - NO INFO ON CONTRACT"						  /*
					  */ 5"At relatives, friends, acquaintances"									  /*
					  */ 6"Rent with other immigrants - WITH CONTRACT" 							      /*
					  */ 7"Rent with other immigrants - WITHOUT CONTRACT"						      /*
					  */ 8"Rent with other immigrants - NO INFO ON CONTRACT"  				   	      /*
					  */ 9"Hotel or guest house" 10"Immigration shelter/center"				          /*
					  */ 11"On the workplace" 12"Squatting"  13"Free housing/concession"          	  /*
					  */ 14"Shack or improvised shelter" 15"Homeless" 16"Other" 99"n/d"
label define livin       1"Alone" 2"Spouse/partner" 3"Spouse/partner + relatives" 4"Relatives"        /*
					  */ 5"Spouse/partner. + friends" 6"Relatives + friends" 						  /*
					  */ 7"Friends/acquaintance " 8"Children" 9"Spouse/partner + children"			  /*
					  */ 10"Spouse/partner + relatives + children" 11"Relatives + children"			  /*
					  */ 12"Spouse/partner + friends + children" 									  /*
					  */ 13"Relatives + friends + children" 14"Friends + children" 99"n/d"
label define nat         1"Same nationality" 2"Italian" 3"Other nationality" 99"n/d"
label define work        1 Unemployed 2 Student 3 Housewife 										  /*
					  */ 4"Legally employed fixed-term"  5"Legally employed part time"			  	  /*
					  */ 6"Legally employed permanent contract"							        	  /*
					  */ 7"Illegally employed but steady" 8"Illegally employed and unsteady" 	  	  /*
					  */ 9"Para-subordinate occupation" 10"Legally self-employed" 		        	  /*
					  */ 11"Illegally self-employed" 12"Entrepreneur" 13"Other" 99"n/d"
label define workr       1"Unemployed/housewife"  2"Student" 3"Legally employed" 					  /*
					  */ 4"Illegally employed" 99"n/d"
label define jb          1"Generic workman" 2"Storage and custody services" 3"Specialized workman"    /*
					  */ 4"Construction worker" 5"Agricultural worker or similar" 6 "Janitor" 		  /*
					  */ 7"Employee" 8"Salesman" 9"Owner of commercial activity" 10"Catering" 		  /*
					  */ 11"Artisan" 12"Transports" 13"Housekeeper/cleaning lady - regular"			  /*
					  */ 14"Housekeeper/cleaning lady - for hire" 15"Home care" 16"Baby sitter"       /*
					  */ 17"Social welfare operator" 18"Doctor or paramedical" 19"Intellectual"		  /*
					  */ 20"Prostitute/viados" 21"Other" 99"n/d"
label define job_find    1 "On my own" 2 "Thanks to a friend/relative, same nationality" 			  /*
					  */ 3"Public service"  5"Private organizaiton"	6"Other" 99"n/d"	
label define job_length  1"<3 months"  2"3-12 months" 3"1-3 years" 4">3 years" 99"n/d			  		  
label define transfer    1"No" 1"Yes, in this municipality" 2"Yes, in this province" 	 			  /* 
					  */ 3"Yes, in Lombardia" 4"Yes, in another region"  5"Yes, in another country"   /*
					  */ 6"Yes, in my home country" 98"Do not know" 99"n/d"	
label define transfer_when  1"As soon as possible" 2 "Within 1 year"  3"Whithin 2-4 years" 			  /*
					  */ 4"Within >5 years" 5"No" 99"n/d"	
label define sanatorie   0"No" 1 "Yes, in 1986/87" 2 "Yes, in 1990/91 (Martelli)" 			  		  /*
					  */ 3"Sì, nel 1995/96 (Dini)" 4"Sì, nel 1998/99 (Turco-Napolitano)" 		  	  /*
					  */ 5"Sì, nel 2002 (Bossi-Fini)" 99"n/d"			  
label define ask_help    1 "Friend in my community" 2 "Italian friends" 3 Pharmacist 				  /*
					  */ 4"State doctor" 5"Doctor from same community" 6"Private doctor"  			  /*
					  */ 7"Voluntary centre" 8"First aid" 9"Nobody" 99"n/d"		
label define reunion_past 1"Never" 2"Yes, I came to Italy to join relatives"					      /*
					  */ 2"Yes, relatives joined me in Italy" 3"I made a request"					  /*
					  */ 4"I made a request and it was rejected" 99"n/d"		
label define reunion_future 1"No" 2"Yes, by my children" 3"Yes, by my spouse" 						  /*
					  */ 4"Yes, by other relatives" 99"n/d"	
label define freq	     1 Always 2 Often 3 Sometimes 4 Never 99"n/d"		
label define freq2	     5 Always 4 Often 3 Sometimes 2 Rarely 1 Never 99"n/d"
label define intensity	 5"Very much" 4 Much 3"A little" 2"Very little" 1"Nothing" 99"n/d"
label define friends	 1"Only migrants" 2"Mostly migrants" 3"Both italians and migrants" 			  /*
					  */ 4"Mostly italians" 5"Only italians" 99"n/d" 
label define foreign_friends 1"Only compatriots" 2"Mostly compatriots" 								  /*
					  */ 3"Foreigners with similar background" 4"Other foreigners" 99"n/d" 
label define df_2006     1"Yes, personally" 2"Yes, a friend/parent did it for me" 					  /*
					  */ 3"Yes, my employer did it for me" 4"No" 99"n/d" 					  
label define df_2007     1"Yes" 2"No" 3"Do not know" 4"Do not know, I would still need a visa" 99"n/d" 
label define reg_status  1"Alwys regular" 2"Always irregular" 3"Irregular then got visa" 			  /*
					  */ 4"Had visa, did not renew" 99"n/d" 				  
label define opinion	 1"Improved" 2"Worsened" 3"Stayed the same" 4"Do not know" 99"n/d"
label define training	 1"No" 2"Yes, compulsory education" 3"Yes, professional formation" 			  /*
					  */ 4"Yes, secondary education" 5"Yes, university level" 6"Yes, firm training"   /*
					  */ 99"n/d"					  
label define opportunities 1"Yes, in any case" 2"Yes, but more effort is required" 3"No" 			  /*
					  */ 4"Do not know" 99"n/d"					  
label define associations 1"Yes, with forigners" 2"Yes, with both italians and foreigners" 	  		  /*
					  */ 3"Yes, with italians" 4"No" 99"n/d"					  
					  

* Label variables
label var   year "Questionnaire year"

replace     province = 15 if province==15146
label value province prov
label var   province "Province of interview"

label var   municipality "Municipality of interview"

label var   w_pro "Weights at the provincial level"

label var   w_reg "Weights at the regional level"

label value gender mf
label var   gender "Gender"

label var   yob "Year of birth"

label value pob pob
label var   pob "Place of birth"

gen         age = year-yob
label var   age "Age"
order       age, after(yob)

replace citizenship = 272 if citizenship==111
replace citizenship = 224 if citizenship==2241
replace citizenship = 351 if citizenship==3511
replace citizenship = 332 if citizenship==3321
replace citizenship = 333 if citizenship==3331 
replace citizenship = 348 if citizenship==3481 
label value citizenship cit
label var   citizenship "Citizenship"

gen         EU10 = 0 if citizenship!=.
replace     EU10 = 1 if citizenship==257 | citizenship==247 | citizenship==244 | citizenship==248 | /*
					 */ citizenship==249 | citizenship==233 | citizenship==255 | citizenship==251
label value EU10 yesno
label var   EU10 "Citizenship from a country joining EU in 2004"
order       EU10, after(citizenship)

gen         EU2 = 0 if citizenship!=.
replace     EU2 = 1 if citizenship==209 | citizenship==235 
label value EU2 yesno
label var   EU2 "Citizenship from a country joining EU in 2007"
order       EU2, after(EU10)

label value civil_status civstat
label var   civil_status "Civil status"

label value religion rel
label var   religion "Religion"   

label value education educ
label var   education "Education"

label value permit perm
label var   permit "Have you got a residency permit (permesso di soggiorno)?"

label value permit_type permt
label var   permit_type "If in possess of a residency permit, please indicate which kind"

label value entry_visa yesno
label var   entry_visa "Did you have a visa when you entered Italy?"

label var   entry_date "Year of arrival in Italy"

gen         years_ita = year-entry_date
label var   years_ita "Number of years since arrival in Italy"
order       years_ita, after(entry_date)

label var   entry_contact "Did you have contacts in Italy before your arrival?"
label value entry_contact yesno

label value civil_registry anagrf
label var   civil_registry "Are you registred at the registry office (anagrafe)?"

label value housing house
label var   housing "Housing"

label value living_with livin
label var   living_with "Who do you live with?"

label value spouse_nationality nat
label var   spouse_nationality "Spouse/partner nationality"

label var   chldrn_tot "Total number of children" 
label var   chldrn_cohab "Number of children living with you "
label var   chldrn_ita "Number of children in Italy"
label var   chldrn_ita_minor "Number of minor children in Italy (<18)"

label var   family_size "Number of people in your family unit in Italy"

replace     family_income =. if family_income==9999
label var	family_income "What is the total net income of your family group?"

replace     family_remittances =. if family_remittances==9999
label var	family_remittances "What is the amount of remittances that your family sends monthly?"

replace 	exp_foodclothes =. if exp_foodclothes>=9999
label var   exp_foodclothes "Average monthly expenditure on food/clothes"

replace 	exp_house =. if exp_house>=9999
label var   exp_house "Average monthly expenditure on housing (rent, mortgage, ...)"

replace 	exp_other =. if exp_other>=9999
label var   exp_other "Average monthly expenditure on other goods"

replace 	exp_remittances =. if exp_remittances>=9999
label var   exp_remittances "Average monthly remittances"

replace 	exp_savings =. if exp_savings>=9999
label var   exp_savings "Average monthly savings"

label value work_status work
label var   work_status "Main working condition"

generate    work_status_reg = work_status
replace     work_status_reg = 1 if work_status==3
replace     work_status_reg = 3 if work_status>3 & work_status<11
replace     work_status_reg = 4 if work_status==7 | work_status==8 | work_status==11 | work_status==12
replace     work_status_reg =. if work_status==13
label value work_status_reg workr
label var   work_status_reg "Working legally?"
order       work_status_reg, after(work_status)

label var   work_hours "How many hours do you work on average each week?"

replace     job = job*10 if job>22 & job<100 
replace     job = job*10 if job==22 & year==2005
tostring    job, force replace
replace     job = "2" if year==2006 & (job=="20" | job=="21" | job=="22")  
replace     job = substr(job,1,2) if strlen(job)==3
replace     job = substr(job,1,1) if strlen(job)==2 & strpos(job,"1")!=1 & job!="20" & job!="21" & job!="22" 
destring    job, force replace
replace 	job = 9999 if job==6 & (year==2003 | year==2004)
replace 	job = 6 if job==5 & (year==2003 | year==2004)
replace 	job = 5 if job==4 & (year==2003 | year==2004)
replace 	job = 4 if job==3 & (year==2003 | year==2004)
replace 	job = 3 if job==2 & (year==2003 | year==2004)
replace 	job = 2 if job==9999 & (year==2003 | year==2004)
replace 	job = 22 if job==13 & (year==2003 | year==2004)
replace 	job = job-1 if job>13 & job<99 & (year==2003 | year==2004)
replace 	job = 21 if job==22
label 		value job jb
label var   job "Job category"

replace     income =. if income>100000
replace  	income =. if income==9999
label var   income "Average net monthly income (from work)" 

label value job_find job_find
label var   job_find "How did you find your current job?"

label value job_length job_length
label var   job_length "How long have you been doing your current job?"

label value transfer transfer
label var   transfer "Do you plan to move elsewhere?"

label value transfer_date transfer_date
label var   transfer_date "When do you plan to move?"

replace 	participation = participation-1 if participation<99
label value participation yesno
label var   participation "Do you think migrants should participate in Italian society and politics?"

label value info_italy intensity
label var   info_italy "Do you stay informed on Italian news?"

label value info_home intensity
label var   info_home "Do you stay informed on your home country news?"

label value	sanatorie sanatorie
label var 	sanatorie "Have you used a sanatoria to regularize your status? Which one?"

label value ask_help ask_help
label var	ask_help "If you feel seek, who would you ask for help?"

label var	income_min "What income should a 4p family need to live decently?"

label value reunification_past reunification_past
label var	reunification_past "Since you are in Italy, have you ever made a family reunification?"

label value reunification_future reunification_future
label var	reunification_future "Do you plan to apply for a family reunification in the next year?"

label value place_worship freq
label var  	place_worship "Do you go to a place of worship to practice your religion?"

label value friends friends
label var	friends "Who are the friends you spend your free time with?"

label value foreign_friends foreign_friends
label var 	foreign_friends "Your foreign friends are:"

label value italian intensity
label var 	italian "How well do you know the italian language?"

label value italian_home freq2
label var 	italian_home "How often do you speak italian at home?"

label value italian_work freq2
label var 	italian_work "How often do you speak italian at work/school?"

label value italian_freetime freq2
label var 	italian_freetime "How often do you speak italian in your free time?"

label value integration_citiz intensity
label var 	integration_citiz "How important italian citizenship is for integration in Italy?"

label value integration_vote intensity
label var 	integration_vote "How important the possibility to vote is for integration in Italy?"

label value integration_title intensity
label var 	integration_title "How important a school title is for integration in Italy?"

label value integration_work intensity
label var 	integration_work "How important fair job opportunities are for integration in Italy?"

label value integration_culture intensity
label var 	integration_culture "How important own culture is for integration in Italy?"

label value integration_language intensity
label var 	integration_language "How important knowing italian is for integration in Italy?"

label value integration_discrim intensity
label var 	integration_discrim "How important not being discriminated is for integration in Italy?"

label value df_2006 df_2006
label var 	df_2006 "Did you apply for decreto-flussi 2006?"

label value df_2007 yesno
label var 	df_2007 "Did you apply for decreto-flussi 2007?"

label value neighbors friends
label var	neighbors "Your neighbors are:"

label value italian_understand intensity
label var 	italian_understand "How well do you understand the italian language?"

label value italian_speak intensity
label var 	italian_speak "How well do you speak the italian language?"

label value italian_read intensity
label var 	italian_read "How well do you read in the italian language?"

label value italian_write intensity
label var 	italian_write "How well do you write in the italian language?"

label value edu_recog edu_recog
label var 	edu_recog "Is your study title recognised in Italy?"

replace		voting = voting-1 if voting<99
label value voting yesno
label var 	voting "If this Sundays there were elections, would you vote?"

label value reg_status reg_status
label var 	reg_status "What has been your juridical status in the past?"

label value integration_migrants opinion
label var 	integration_migrants "Since you arrived in Italy, how has attitude towards migrants changed?"

label value integration_school opinion
label var 	integration_school "How has attitude towards migrants in schools changed?"

label value integration_children opinion
label var 	integration_children "How has attitude towards your children in schools changed?"

label value integration_minors opinion
label var 	integration_minors "How has attitude towards migrant minors in schools changed?"

label var 	marr_year "Year of current marriage/union"

label var 	spouse_yob "Year of birth of the spouse"

label var 	spouse_yofentry "When did your spouse/partner enter Italy?"

replace 	spouse_workstat = spouse_workstat-1 if spouse_workstat<99
label value spouse_workstat yesno
label var 	spouse_workstat "Does your partner/spouse living in Italy work?"

label value training training
label var 	training "Are you currently attending professional training courses in Italy?"

label value opportunities opportunities
label var 	opportunities "In Italy do you think you have the same opportunities locals have?"

label value associations associations
label var 	associations "Do you actively participate in associations in Italy?"


save "$ismu\ISMU.dta", replace





